package com.neuedu.oasystem.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.neuedu.oasystem.dao.IRoleDao;

import com.neuedu.oasystem.entity.Role;
import com.neuedu.oasystem.util.DBHelper;

public class RoleDaoImpl extends BaseDao implements IRoleDao{

	@Override
	public boolean add(Role param) {
		Connection conn = DBHelper.openConnection();
		PreparedStatement stmt = null;
		try {
			String sql ="insert into role (role_id,rolo_name,role_description) values(?,?,?)";
			stmt = conn.prepareStatement(sql);
			stmt.setInt(1, param.getRoleId());
			stmt.setString(2, param.getRoleName());
			stmt.setString(3, param.getRoleDescription());
			int count = stmt.executeUpdate();
			if (count == 1) {
				return true;
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBHelper.closeAll(null, stmt, conn);
		}
		return false;
	}

	@Override
	public boolean edit(Role param) {
		Connection conn = DBHelper.openConnection();
		PreparedStatement stmt = null;
		try {
			String sql = "update role set rolo_name=?,role_description=? where role_id=?";
			stmt =conn.prepareStatement(sql);
			stmt.setString(1, param.getRoleName());
			stmt.setString(2, param.getRoleDescription());
			stmt.setInt(3, param.getRoleId());
			int count = stmt.executeUpdate();
			if (count == 1) {
				return true;
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBHelper.closeAll(null, stmt, conn);
		}
		return false;
	}
		

	@Override
	public boolean deleteById(int id) {
		Connection conn = DBHelper.openConnection();
		PreparedStatement stmt = null;

		try {
			String sql = "delete from role where role_id=?";
			stmt = conn.prepareStatement(sql);
			stmt.setInt(1, id);
			int count = stmt.executeUpdate();
			if (count == 1) {
				return true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBHelper.closeAll(null, stmt, conn);
		}

		return false;
	}

	@Override
	public Role findById(int id) {
		
		return null;
	}

	@Override
	public List<Role> findAll() {
		List<Role> list = new ArrayList<Role>();
		Connection conn = DBHelper.openConnection();
		PreparedStatement stmt = null;
		ResultSet rs = null;

		try {
			conn = getConnection();
			String sql = "select role_id,rolo_name,role_description from role ";
			stmt = conn.prepareStatement(sql);
			rs = stmt.executeQuery();
			while (rs.next()) {
				int roleId = rs.getInt(1);
				String roleName = rs.getString("rolo_name");
				String roleDescription = rs.getString("role_description");				
				Role role = new Role(roleId,roleName,roleDescription);
				list.add(role);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBHelper.closeAll(rs, stmt, conn);
		}
		return list;
	}
	@Override
	public List<Role> findByExample(Role param) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Role> findByPager(int page, int size) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int deleteByIds(int[] ids) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int addAll(List<Role> useparamrs) {
	
		return 0;
	}

	@Override
	public int editAll(List<Role> param) {
		
		return 0;
	}

}
